import moment from 'moment';

// 选择框把选中的数据排第一
export function checkOneList(value, list, key = 'id') {
	let filterList = [];
	let oneObj = {};
	list.map(item => {
		if (item[key] == value) {
			oneObj = item;
		} else {
			filterList.push(item);
		}
	});
	return [oneObj, ...filterList];
}
// timeDiff 对比时间间隔  传入开始时间和结束时间  type 为 moment 对于的类型
export function timeDiff(start, end, type = 'days') {
	let a = moment(start);
	let b = moment(end);
	return b.diff(a, type);
}

// dateFormat 用于处理日期间隔  传入 数字 返回对应的开始 结束时间
export function dateFormat(val) {
	let start = null;
	let end = null;
	let d;
	switch (val) {
		case 0:
			// 今天
			start = moment()
				.hour(0)
				.minute(0)
				.second(0)
				.format('YYYY-MM-DD HH:mm:ss');

			end = moment().format('YYYY-MM-DD HH:mm:ss');
			break;
		case 1:
			// 昨天
			d = moment().subtract(1, 'day');

			start = d
				.hour(0)
				.minute(0)
				.second(0)
				.format('YYYY-MM-DD HH:mm:ss');
			end = d
				.hour(23)
				.minute(59)
				.second(59)
				.format('YYYY-MM-DD HH:mm:ss');
			break;
		case 2:
			// 仅七天
			start = moment()
				.subtract(6, 'day')
				.hour(0)
				.minute(0)
				.second(0)
				.format('YYYY-MM-DD HH:mm:ss');

			end = moment()
				.hour(23)
				.minute(59)
				.second(59)
				.format('YYYY-MM-DD HH:mm:ss');
			break;
		case 3:
			// 近三十天
			start = moment()
				.subtract(29, 'day')
				.hour(0)
				.minute(0)
				.second(0)
				.format('YYYY-MM-DD HH:mm:ss');

			end = moment()
				.hour(23)
				.minute(59)
				.second(59)
				.format('YYYY-MM-DD HH:mm:ss');
			break;
		case 4:
			// 本月
			start = moment().format('YYYY-MM-01 00:00:00');

			end = moment()
				.hour(23)
				.minute(59)
				.second(59)
				.format('YYYY-MM-DD HH:mm:ss');
			break;
	}
	return {
		startTime: start,
		endTime: end
	};
}

//  formatDate 传入毫秒数 返回 时分秒
export function formatDate(num = 0) {
	num = num / 1000;
	let house = 60 * 60;
	let minute = 60;
	let h = '00';
	let m = '00';
	let s = '00';
	// 小时数
	let tiemDay = parseInt(num / house);
	if (tiemDay) {
		h = tiemDay;
		// 分钟数
		let dnum = num - house * tiemDay;
		if (dnum) {
			m = parseInt(dnum / minute);

			if (dnum - m * minute) {
				s = parseInt(dnum - m * minute);
			} else {
				s = 0;
			}
		}
	} else {
		m = parseInt(num / minute);

		if (num - m * minute) {
			s = parseInt(num - m * minute);
		} else {
			s = 0;
		}
	}
	if (m < 10) {
		if (m == '00') {
			m = '00';
		} else {
			m = '0' + m;
		}
	}
	if (s < 10) {
		if (s == '00') {
			s = '00';
		} else {
			s = '0' + s;
		}
	}
	return `${h}小时${m}分${s}秒`;
}
export function download({ data, fileName }) {
	const arraybuffer = base64ToArrayBuffer(data);
	let url = window.URL.createObjectURL(new Blob([arraybuffer]));
	let link = document.createElement('a');
	link.style.display = 'none';
	link.href = url;
	link.setAttribute('download', `${fileName}.xlsx`);
	0;
	document.body.appendChild(link);
	link.click();
}
function base64ToArrayBuffer(base64) {
	var binary_string = window.atob(base64);
	var len = binary_string.length;
	var bytes = new Uint8Array(len);
	for (var i = 0; i < len; i++) {
		bytes[i] = binary_string.charCodeAt(i);
	}
	return bytes.buffer;
}
